﻿

using CCSSolderUI.Common.Event;
using CCSSolderUI.Models;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.IO;
using System.Windows.Forms;

namespace CCSSolderUI.ViewModels.Dialogs
{
    public class MParamDialogViewModel : BindableBase, IDialogAware
    {
        #region 变量
        Param param;
        IEventAggregator _eventAggregator;
        string PartNumber;
        #endregion
        #region 属性绑定
        private bool isScanBarcode;
        public bool IsScanBarcode
        {
            get { return isScanBarcode; }
            set { SetProperty(ref isScanBarcode, value); }
        }
        private bool isLazer;
        public bool IsLazer
        {
            get { return isLazer; }
            set { SetProperty(ref isLazer, value); }
        }
        private string imagePath;
        public string ImagePath
        {
            get { return imagePath; }
            set { SetProperty(ref imagePath, value); }
        }
        private string comparisonPath;
        public string ComparisonPath
        {
            get { return comparisonPath; }
            set { SetProperty(ref comparisonPath, value); }
        }
        private bool isMES;
        public bool IsMES
        {
            get { return isMES; }
            set { SetProperty(ref isMES, value); }
        }
        private string mESPath;
        public string MESPath
        {
            get { return mESPath; }
            set { SetProperty(ref mESPath, value); }
        }
        private string eZDFile;
        public string EZDFile
        {
            get { return eZDFile; }
            set { SetProperty(ref eZDFile, value); }
        }
        private int barCodeLength1;
        public int BarCodeLength1
        {
            get { return barCodeLength1; }
            set {
                if (value > 0 && value < 4)
                {
                    SetProperty(ref barCodeLength1, value);
                }
            }
        }
        private int barCodeLength2;
        public int BarCodeLength2
        {
            get { return barCodeLength2; }
            set
            {
                if (value > 0 && value < 4)
                {
                    SetProperty(ref barCodeLength2, value);
                }
            }
        }
        #endregion
        #region 导航
        public string Title => "";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            PartNumber = parameters.GetValue<string>("PartNumber");
            LoadParam(PartNumber);
        }
        #endregion
        #region 方法绑定
        private DelegateCommand<object> openDirectoryCommand;
        public DelegateCommand<object> OpenDirectoryCommand =>
            openDirectoryCommand ?? (openDirectoryCommand = new DelegateCommand<object>(ExecuteOpenDirectoryCommand));
        private DelegateCommand saveButtonCommand;
        public DelegateCommand SaveButtonCommand =>
            saveButtonCommand ?? (saveButtonCommand = new DelegateCommand(ExecuteSaveButtonCommand));
        private DelegateCommand<object> selectFileCommand;
        public DelegateCommand<object> SelectFileCommand =>
            selectFileCommand ?? (selectFileCommand = new DelegateCommand<object>(ExecuteSelectFileCommand));

        void ExecuteSelectFileCommand(object obj)
        {
            using (var dlg = new OpenFileDialog())
            {
                switch (obj.ToString())
                {
                    case "EZDFile":
                        dlg.Filter = "选择模板(*.ezd)|*.ezd*";
                        break;
                    default:
                        break;
                }
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    switch (obj.ToString())
                    {
                        case "EZDFile":
                            EZDFile = dlg.FileName;
                            break;
                        default:
                            break;
                    }
                }
            }
           
        }
        void ExecuteSaveButtonCommand()
        {
            try
            {
                param.IsScanBarcode = IsScanBarcode;
                param.IsLazer = IsLazer;
                param.ImagePath = ImagePath;
                param.ComparisonPath = ComparisonPath;
                param.EZDFile = EZDFile;
                param.IsMES = IsMES;
                param.MESPath = MESPath;
                param.BarCodeLength1 = BarCodeLength1;
                param.BarCodeLength2 = BarCodeLength2;
                string jsonString = JsonConvert.SerializeObject(param, Formatting.Indented);
                File.WriteAllText(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, PartNumber, "Param.json"), jsonString);
                MessageBox.Show("保存完成", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                _eventAggregator.GetEvent<MessagePrintEvent>().Publish(new Common.Event.Message() { Sender = "MParamDialog", Type = "Update", Content = null });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                _eventAggregator.GetEvent<MessagePrintEvent>().Publish(new Common.Event.Message() { Sender = "MParamDialog", Type = "Print", Content = ex.Message });
            }
        }
        void ExecuteOpenDirectoryCommand(object obj)
        {
            using (var fbd = new FolderBrowserDialog())
            {
                System.Windows.Forms.DialogResult result = fbd.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
                {
                    switch (obj.ToString())
                    {
                        case "Image":
                            ImagePath = fbd.SelectedPath;
                            break;
                        case "Comparison":
                            ComparisonPath = fbd.SelectedPath;
                            break;
                        default:
                            break;
                    }
                }
            }

        }
        #endregion
        #region 构造函数
        public MParamDialogViewModel(IEventAggregator ea)
        {
            _eventAggregator = ea;
            _eventAggregator.GetEvent<MessagePrintEvent>().Subscribe(MessageReceived);
        }
        #endregion
        #region 功能函数
        private void MessageReceived(Common.Event.Message obj)
        {
            switch (obj.Type)
            {
                case "Select":
                    PartNumber = obj.Content;
                    LoadParam(PartNumber);
                    break;
                case "Update":
                    if (obj.Sender != "MParamDialog")
                    {
                        LoadParam(PartNumber);
                    }
                    break;
                default:
                    break;
            }
        }
        void LoadParam(string partNumber)
        {
            try
            {
                //Json序列化，从文件读取
                string jsonString = File.ReadAllText(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, partNumber, "Param.json"));
                param = JsonConvert.DeserializeObject<Param>(jsonString);
                IsScanBarcode = param.IsScanBarcode;
                IsLazer = param.IsLazer;
                ImagePath = param.ImagePath;
                ComparisonPath = param.ComparisonPath;
                EZDFile = param.EZDFile;
                IsMES = param.IsMES;
                MESPath = param.MESPath;
                BarCodeLength1 = param.BarCodeLength1;
                BarCodeLength2 = param.BarCodeLength2;
            }
            catch (Exception ex)
            {
                _eventAggregator.GetEvent<MessagePrintEvent>().Publish(new Common.Event.Message() { Sender = "MParamDialog", Type = "Print", Content = ex.Message });
            }
        }
        #endregion
    }
}
